<!DOCTYPE html>
<html>
<head>
	<title>async 继发与并发</title>
</head>
<body>
  async 异步函数
  await后面是一个Promise对象，返回该对象的结果，如果不是Promise对象，就直接返回对应的值。
继发：一个接口返回后才会发送另一个请求
<script type="text/javascript">
// const some1 = new Promise();
// const some2 = new Promise();
// 	async function aaa(){
//   let a = await some1()
//   let b = await some2()
// //这样的写法是继发的 相当于A执行完毕后再执行B
// }
// async function aaa(){
//   let a = some1()
//   let b = some2()
//   let aDone = await a
//   let bDone = await b
// //这样的写法是并发的 无先后顺序
// }





// 并发的两种写法
// 写法一
// let [foo, bar] = await Promise.all([getFoo(), getBar()]);

// // 写法二
// let fooPromise = getFoo();
// let barPromise = getBar();
// let foo = await fooPromise;
// let bar = await barPromise;

// 例子：
// 3秒后打印
const timeout = async function(ms){
  await new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}
const asyncPrint = async function(value, ms){
  try {
    await timeout(ms);
  } catch (error) {
    console.log(err);
  }
  
  console.log(value);
}
asyncPrint('hello',3000)



var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve("slow");
      console.log("slow promise is done");
    }, 2000);
  });
};

var resolveAfter1Second = function() {
  console.log("starting fast promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve("fast");
      console.log("fast promise is done");
    }, 1000);
  });
};

var sequentialStart = async function() {
  console.log('==队列SEQUENTIAL START==');

  // 1. Execution gets here almost instantly
  const slow = await resolveAfter2Seconds();
  console.log(slow); // 2. this runs 2 seconds after 1.

  const fast = await resolveAfter1Second();
  console.log(fast); // 3. this runs 3 seconds after 1.
}

var concurrentStart = async function() {
  console.log('==同时CONCURRENT START with await==');
  const slow = resolveAfter2Seconds(); // starts timer immediately
  const fast = resolveAfter1Second(); // starts timer immediately

  // 1. Execution gets here almost instantly
  console.log(await slow); // 2. this runs 2 seconds after 1.
  console.log(await fast); // 3. this runs 2 seconds after 1., immediately after 2., since fast is already resolved
}

var concurrentPromise = function() {
  console.log('==同时CONCURRENT START with Promise.all==');
  return Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then((messages) => {
    console.log(messages[0]); // slow
    console.log(messages[1]); // fast
  });
}

var parallel = async function() {
  console.log('==PARALLEL with await Promise.all==');
  
  // Start 2 "jobs" in parallel and wait for both of them to complete并行启动2个“作业”并等待它们完成
  await Promise.all([
      (async()=>console.log(await resolveAfter2Seconds()))(),
      (async()=>console.log(await resolveAfter1Second()))()
  ]);
}

// This function does not handle errors. See warning below!
var parallelPromise = function() {
  console.log('==PARALLEL with Promise.then==');
  resolveAfter2Seconds().then((message)=>console.log(message));
  resolveAfter1Second().then((message)=>console.log(message));
}

// sequentialStart(); // after 2 seconds, logs "slow", then after 1 more second, "fast"

// wait above to finish
// setTimeout(concurrentStart, 4000); // after 2 seconds, logs "slow" and then "fast"

// wait again
// setTimeout(concurrentPromise, 7000); // same as concurrentStart

// wait again
// setTimeout(parallel, 10000); // truly parallel: after 1 second, logs "fast", then after 1 more second, "slow"

// wait again
setTimeout(parallelPromise, 13000); // same as parallel

</script>

</body>
</html>